package com.sl.au.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ContractBillInfo;
import com.sl.au.entity.Module;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.service.AccountsReceivableService;
import com.sl.au.service.OptionService;
import com.sl.au.service.PaymentSearchForViewService;
import com.sl.au.service.SaleAreaService;
import com.sl.utils.ExdeUtil;

/**
 * 合同应收账查询控制器
 */
@RestController
@RequestMapping(value="/ui/accountsreceivable")
public class AccountsReceivableController extends BaseController{
	@Autowired
	AccountsReceivableService accountsReceivableService;
	//片区
		@Autowired
		SaleAreaService saleAreaService;
		
		@Autowired
		OptionService optionService;
	
	 @RequestMapping(method = RequestMethod.GET)
	    public ModelAndView index(){
	        ModelAndView modelAndView=createLayoutView("accountsreceivable/accountsreceivableindex");
	        String[] areaNames = null;
			String[] superiors = null;
			if(StringUtils.hasText(request.getParameter("areaName"))){
				areaNames = request.getParameter("areaName").toString().split(",");//片区
			}
			if(StringUtils.hasText(request.getParameter("deptName"))){
				superiors = request.getParameter("deptName").toString().split(",");//部门
			}
			List<SaleArea> orderAreas = saleAreaService.getRepository().getAllSaleArea();
			List<SaleArea> SaleOrderAreas = new ArrayList<SaleArea>();//销售
			List<SaleArea> CompleteOrderAreas = new ArrayList<SaleArea>();//成套
			List<SaleArea> ForeignOrderAreas = new ArrayList<SaleArea>();//外贸
			for(SaleArea orderArea : orderAreas){
				
				orderArea.setIsSelected(false);
				if(orderArea.getSuperior().equals("销售处")){
					if(superiors!=null&&superiors.length>0){
						if(areaNames!=null&&areaNames.length>0){
							for(int i=0;i<areaNames.length;i++){
								if(areaNames[i].equals(orderArea.getSuperiorArea()))
									orderArea.setIsSelected(true);
							}
						}else{
							for(int i=0;i<superiors.length;i++){
								if(superiors[i].equals(orderArea.getSuperior()))
									orderArea.setIsSelected(true);
							}
						}
					}
					if(orderArea.getAreaName().equals("销售处-分管领导")){
					}else{
						if(orderArea.getAreaName().equals("科长")){
						}else{
							SaleOrderAreas.add(orderArea);
						}
					}
				}
				if(orderArea.getSuperior().equals("成套处")){
					if(superiors!=null&&superiors.length>0){
						if(areaNames!=null&&areaNames.length>0){
							for(int i=0;i<areaNames.length;i++){
								if(areaNames[i].equals(orderArea.getSuperiorArea()))
									orderArea.setIsSelected(true);
							}
						}else{
							for(int i=0;i<superiors.length;i++){
								if(superiors[i].equals(orderArea.getSuperior()))
									orderArea.setIsSelected(true);
							}
						}
					}
					if(orderArea.getAreaName().equals("其他")){
					}else{
						if(orderArea.getAreaName().equals("成套处分管领导")){
						}else{
							CompleteOrderAreas.add(orderArea);
						}
					}
				}
				if(orderArea.getSuperior().equals("外贸处")){
					if(superiors!=null&&superiors.length>0){
						if(areaNames!=null&&areaNames.length>0){
							for(int i=0;i<areaNames.length;i++){
								if(areaNames[i].equals(orderArea.getSuperiorArea()))
									orderArea.setIsSelected(true);
							}
						}else{
							for(int i=0;i<superiors.length;i++){
								if(superiors[i].equals(orderArea.getSuperior()))
									orderArea.setIsSelected(true);
							}
						}
					}
						if(orderArea.getAreaName().equals("外贸处-分管领导")){
						}else{
							ForeignOrderAreas.add(orderArea);
						}
				}
			}
			modelAndView.addObject("SaleOrderAreas", SaleOrderAreas);
			modelAndView.addObject("CompleteOrderAreas", CompleteOrderAreas);
			modelAndView.addObject("ForeignOrderAreas", ForeignOrderAreas);
			
			modelAndView.addObject("industryInfos", optionService.getRepository().findByGroup("行业"));//行业
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
			modelAndView.addObject("startDate", request.getParameter("beginDate"));//开始时间
			modelAndView.addObject("endDate", request.getParameter("endDate")!=null ? request.getParameter("endDate") : df.format(new Date()));//结束时间
			
			if(request.getParameter("zdType")==null && request.getParameter("hk")==null){
				modelAndView.addObject("zdType", "应收账金额");//
				modelAndView.addObject("hk", "");//回款类型 
			}else{
				modelAndView.addObject("zdType", request.getParameter("zdType"));//
				modelAndView.addObject("hk", request.getParameter("hk")!=null ? request.getParameter("hk") : "");//回款类型
			}
			
			modelAndView.addObject("module", module);
	        return modelAndView;
	    }
	
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		Map<String, Object>numMap=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String zdType = request.getParameter("zdType");
		String hk = request.getParameter("hk");
		System.out.println(hk);
		String monthYear = ExdeUtil.findYearMonth();
		List<ContractBillInfo> contractBillInfo = null;
		Account a = this.getMyAccount();
		User user = a.getUser();
		Module module = this.getPageModule("contractInfo");
		String dataFilter = module.getDataFilter();
		this.getQueryParams(qMap,numMap);
		//System.out.println(numMap);
		condition.put("zdType", zdType);
		condition.put("hk", hk);
		condition.put("monthYear", monthYear);
		condition.put("dataFilter", dataFilter);
		condition.put("qMap", qMap);
		condition.put("numMap", numMap);
		contractBillInfo = accountsReceivableService.getPayHistory(start, length,condition,user,null,null);
		long count =0;		
		count=accountsReceivableService.getPayHistory(0, Integer.MAX_VALUE,condition,user,null,null).size();
		map.put("data", contractBillInfo);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	@RequestMapping(value = "getSum", method = RequestMethod.GET)
	public Map<String, Object> getSum(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		Map<String, Object>numMap=new HashMap<String, Object>();
		String zdType = request.getParameter("zdType");
		String hk = request.getParameter("hk");
		String sum = request.getParameter("sum");
		System.out.println(hk);
		Account a = this.getMyAccount();
		User user = a.getUser();
		Module module = this.getPageModule("contractInfo");
		String dataFilter = module.getDataFilter();
		this.getQueryParams(qMap,numMap);
		//System.out.println(numMap);
		condition.put("zdType", zdType);
		condition.put("hk", hk);
		condition.put("sum", sum);
		condition.put("dataFilter", dataFilter);
		condition.put("qMap", qMap);
		condition.put("numMap", numMap);
		Double data = accountsReceivableService.getSum(condition);
		//System.out.println(data);
		if(data!=null)
			map.put("data", new java.text.DecimalFormat("#.00").format(data));
		else
			map.put("data", 0);
		return map;
	}
	
	private void getQueryParams(Map<String, Object> qMap,Map<String, Object>numMap){
		String names[]={"cOrderArea","industryInfo","state","contractType"};
		String dates[] = {"schedulePayDate"};
		for(String name:names){
			System.out.println(request.getParameter(name));
			if(StringUtils.hasText(request.getParameter(name))){
				String[] a = request.getParameter(name).toString().split(",");
				String b = "";
				for(int i=0;i<a.length;i++){
					b += "'" + a[i] + "',";
				}
				b = b.substring(0, b.length()-1);
				numMap.put(name, b);
			}
		}
		for(String date:dates){
			if(null!=this.getDateQueryItem(date)){
				qMap.put(date,this.getDateQueryItem(date));
			}
		}
		if(StringUtils.hasText(request.getParameter("totalSum"))){
			numMap.put("totalSum", request.getParameter("totalSum").toString());
		}
		if(StringUtils.hasText(request.getParameter("Contract"))){
			numMap.put("Contract", request.getParameter("Contract").toString());
		}
		if(StringUtils.hasText(request.getParameter("cOrderUnit"))){
			System.out.println(request.getParameter("cOrderUnit"));
			numMap.put("cOrderUnit", request.getParameter("cOrderUnit").toString());
		}
	}
}
